#ifndef SILENTMOUSEOPERATION_H
#define SILENTMOUSEOPERATION_H

#include "SilentMouseOperation_global.h"
#include <windows.h>
#include <shlobj.h>
#include <unknwn.h>  // 包含 IUnknown 接口定义
#include <objidl.h>  // 包含 IDataObject 和 IDropSource 接口定义
#include <vector>
#include <string>

// 自定义 IDataObject 实现
class FileDataObject : public IDataObject {
public:
    FileDataObject(const std::vector<std::wstring>& files, int x, int y);
    virtual ~FileDataObject();

    // IUnknown 方法 - 必须实现
    STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject) override;
    STDMETHOD_(ULONG, AddRef)() override;
    STDMETHOD_(ULONG, Release)() override;

    // IDataObject 方法
    STDMETHOD(GetData)(FORMATETC* pFormatetc, STGMEDIUM* pMedium) override;
    STDMETHOD(GetDataHere)(FORMATETC* pFormatetc, STGMEDIUM* pMedium) override;
    STDMETHOD(QueryGetData)(FORMATETC* pFormatetc) override;
    STDMETHOD(GetCanonicalFormatEtc)(FORMATETC* pFormatetcIn, FORMATETC* pFormatetcOut) override;
    STDMETHOD(SetData)(FORMATETC* pFormatetc, STGMEDIUM* pMedium, BOOL fRelease) override;
    STDMETHOD(EnumFormatEtc)(DWORD dwDirection, IEnumFORMATETC** ppEnumFormatEtc) override;
    STDMETHOD(DAdvise)(FORMATETC* pFormatetc, DWORD advf, IAdviseSink* pAdvSink, DWORD* pdwConnection) override;
    STDMETHOD(DUnadvise)(DWORD dwConnection) override;
    STDMETHOD(EnumDAdvise)(IEnumSTATDATA** ppEnumAdvise) override;

private:
    ULONG m_refCount;  // 引用计数变量
    std::vector<std::wstring> m_files;
    int m_targetX;     // 目标X坐标
    int m_targetY;     // 目标Y坐标
};

// 自定义 IDropSource 实现
class FileDropSource : public IDropSource {
public:
    FileDropSource();
    virtual ~FileDropSource();

    // IUnknown 方法 - 必须实现
    STDMETHOD(QueryInterface)(REFIID riid, void** ppvObject) override;
    STDMETHOD_(ULONG, AddRef)() override;
    STDMETHOD_(ULONG, Release)() override;

    // IDropSource 方法
    STDMETHOD(QueryContinueDrag)(BOOL fEscapePressed, DWORD grfKeyState) override;
    STDMETHOD(GiveFeedback)(DWORD dwEffect) override;
    
    // 获取QueryContinueDrag调用次数
    int GetQueryContinueDragCount() const { return m_callCount; }

private:
    ULONG m_refCount;  // 引用计数变量
    bool m_firstCall;  // 标记是否是第一次调用QueryContinueDrag，用于静默拖放支持
    int m_callCount;   // 当前调用计数，用于静默拖放时的模拟操作
    int m_maxCalls;    // 最大调用次数，超过此值将强制触发放置操作
};

// 鼠标点击类型枚举
enum MouseClickType {
    LEFT_CLICK = 0,
    RIGHT_CLICK = 1,
    MIDDLE_CLICK = 2,
    LEFT_DOUBLE_CLICK = 3,
    RIGHT_DOUBLE_CLICK = 4,
    MIDDLE_DOUBLE_CLICK = 5
};

// 导出函数声明
extern "C" {
SILENTMOUSEOPERATION_API bool SilentDragDropFiles(
    const wchar_t** files,
    int fileCount,
    int targetX,
    int targetY
    );
    
SILENTMOUSEOPERATION_API bool SilentMouseClick(
    int targetX,
    int targetY,
    MouseClickType clickType = LEFT_CLICK
    );
}

#endif // SILENTMOUSEOPERATION_H
